/**
 * 表单基础逻辑
 * */
import {
	format
} from '@/libs/common.js';
import dictMixins from './dictMixins.js';
import http from './request.js'
import {
	mapMutations
} from 'vuex';
export default {
	mixins: [dictMixins],
	name: 'approvalMixins',
	data() {
		return {
			// 视图模型
			formModel: {},
			// 实时同步
			syncColumn: [],
			// 表单事件
			formEvent: {}
		}
	},
	mounted() {
		this.formEvent.columnItemChange = () => {};
	},
	methods: {
		...mapMutations({
			// 保存页面临时参数
			savePageTempParams: 'savePageTempParams',
			claearPageTempParams:'claearPageTempParams'
		}),
		/**
		 * 根据key返回对应的列表对象
		 * @param {Arrar} 	column 	列表
		 * @param {String} 	key 	待查找项对应key
		 * @param {String} 	value 	待查找项对应value
		 * */
		getColumnItemByKeyAndValue(column, key, value) {
			return column.filter(item => {
				return item[key] == value
			})[0];
		},
		/**
		 * 将选项数据转换成下拉框组件可用的选项列表
		 * @param {Object} resp		接口返回数据 
		 * @param {Object} config	转换的配置
		 * nameKey	名称key
		 * valueKey 数值key
		 * */
		getChoicesByResp(resp, config) {
			let {
				nameKey,
				valueKey
			} = config;
			let choices = resp.result.map(item => {
				item.name = item[nameKey];
				item.value = item[valueKey];
				return item;
			})
			return choices;
		},
		/**
		 * 使用vuex-store中的缓存参数更新当前页面参数
		 * */
		updateParamsWithCash() {
			let pageTempParams = this.$store.state.pageTempParams;
			if (pageTempParams) {
				let {
					formModel,
					syncColumn
				} = pageTempParams
				this.formModel = formModel;
				this.column = this.syncColumn = syncColumn;
			}else{
				this.column.forEach((item,index)=>{
					this.column[index].value =""
				})
				this.changeColumn(this.column)
			}
		},
		/**
		 * 表单column更新事件
		 * @param {Array} 	column 		视图模型
		 * */
		changeColumn(column) {
			this.syncColumn = Object.assign([], column, []);
			column.map(formItem => {
				let key = formItem.name;
				let value = formItem.value;
				let obj = {};
				obj[key] = value;
				this.EventBus.emit('columnItemChange', {
					key,
					value
				});
				this.formModel = Object.assign(this.formModel, obj);
			});
		},
		/**
		 * 将表单数据赋值给视图模型
		 * @param {Object} 	formData 	表单数据
		 * @param {Array} 	column 		视图模型
		 * */
		assignFormDataToViewModel(formData, column) {
			column.map(formItem => {
				this.assignFormItem(formData, formItem);
			})
		},
		/**
		 * 赋值表单单项
		 * @param {Object} 	formData 		表单数据
		 * @param {Array} 	formItem 		表单单项
		 * */
		assignFormItem(formData, formItem) {
			let caseMaps = [{
					condition() { // 普通输入框
						return formItem.type == 'normalInput'
					},
					func() {
						let dataValue = formData[formItem.name];
						formItem.value = dataValue || '';
					}
				},
				{
					condition() { // 下拉选项框
						return formItem.type == 'picker' && formItem.mode == 'selector'
					},
					func() {
						let dataValue = formData[formItem.name];
						let findOne, indexValue;
						console.log(dataValue,formItem)
						formItem.range.map((choice, index) => {
							if (choice.value == dataValue) {
								indexValue = index;
								findOne = choice
							}
						})
						if (findOne) {
							formItem.value = indexValue;
							formItem.text = findOne.name;
						}
					}
				},
				{
					condition() { // 时间选项框
						return formItem.type == 'picker'&& formItem.mode !== 'selector'
					},
					func() {
						let dataValue = formData[formItem.name];
						if (!dataValue) return
						formItem.value = dataValue;
						formItem.text = dataValue;
					}
				},
				{
					condition() { // 图片
						return formItem.type == 'upload'
					},
					func() {
						formItem.attachmentVoListStr = formData.attachmentVoListStr;
					}
				},
				{
					condition() { 
						return formItem.type == 'textarea'
					},
					func() {
						formItem.value = formData.lessonCondition;
					}
				}
			];
			let findOne = caseMaps.filter(item => {
				return item.condition();
			})[0]
			if (!findOne) return;
			findOne.func();
			
		},
		//将 表单column 转化为键值对
		transColumnToObj(column) {
			if (column.length == 0) return
			let obj = {}
			column.forEach(item => {
				obj[item.name] = item.value
			})
			return obj
		},
		//赋值给column指定项
		fetchDataToColumn(key, val) {
			this.column.forEach((item, i) => {
				if (item['name'] == key) {
					this.column[i].value = val
				}
			})
		},
		/**
		 * 表单模型是否通过校验规则
		 * @param {Object} 	formModel 	表单模型
		 * @param {Array} 	rules 		校验规则
		 * @example rules: [{key: 'a', checkFunc: function() {}, errMsg: ''}]	
		 * key: 待校验key	checkFuc: 校验方法-返回true则校验通过	errMsg:	校验不通过的提示
		 * @return {Object} {isPass: true, errMsg: ''} || {isPass: false, errMsg: '不通过提示'}
		 * */
		checkFormIsPassRules(formModel, rules) {
			let unPassRules = rules.filter(rule => {
				let checkIsPass = rule.checkFunc(formModel[rule.key])
				return !checkIsPass;
			})
			if (unPassRules.length > 0) {
				return {
					isPass: false,
					errMsg: unPassRules[0].errMsg
				}
			}
			return {
				isPass: true
			};
		}
	},
}
